﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.ExternalConnections.BrowseControl
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using DnProcessSimulateCommands.ExternalConnections.Res;
using Opc.Ua;
using Opc.UA.Server.API;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.Ui;


namespace DnProcessSimulateCommands.ExternalConnections;

public class BrowseControl : UserControl
{
  private OpcUAServer m_Server;
  private bool m_RebrowseOnExpandNode;
  private TreeNode m_CurrentNode;
  private IContainer components;
  private TreeView tvBrowseTree;
  private ImageList TreeViewIL;

  public event BrowseControl.SelectionChangedEventHandler SelectionChanged;

  public void OnSelectionChanged(TreeNode node)
  {
    if (this.SelectionChanged == null)
      return;
    this.SelectionChanged(node);
  }

  public event BrowseControl.UpdateStatusLabelEventHandler UpdateStatusLabel;

  public void OnUpdateStatusLabel(string strMessage, bool bSuccess)
  {
    if (this.UpdateStatusLabel == null)
      return;
    this.UpdateStatusLabel(strMessage, bSuccess);
  }

  public BrowseControl() => this.InitializeComponent();

  public bool RebrowseOnNodeExpande
  {
    get => this.m_RebrowseOnExpandNode;
    set => this.m_RebrowseOnExpandNode = value;
  }

  public OpcUAServer Server
  {
    get => this.m_Server;
    set => this.m_Server = value;
  }

  public int Browse(TreeNode parentNode)
  {
    NodeId nodeId;
    TreeNodeCollection nodes;
    if (parentNode == null)
    {
      nodeId = new NodeId(84U, (ushort) 0);
      nodes = this.tvBrowseTree.Nodes;
    }
    else
    {
      ReferenceDescription tag = (ReferenceDescription) parentNode.Tag;
      if (tag == null)
        return -1;
      nodeId = new NodeId(tag.NodeId.Identifier, tag.NodeId.NamespaceIndex);
      nodes = parentNode.Nodes;
    }
    int num1 = 0;
    Cursor.Current = Cursors.WaitCursor;
    if (this.m_RebrowseOnExpandNode || parentNode == null || !parentNode.Checked)
    {
      parentNode?.Nodes.Clear();
      try
      {
        ReferenceDescriptionCollection descriptionCollection;
        num1 = this.m_Server.Browse(nodeId, ref descriptionCollection);
        if (descriptionCollection == null)
          return -1;
        if (num1 == 0)
        {
          this.tvBrowseTree.BeginUpdate();
          if (parentNode != null)
            parentNode.Checked = true;
          foreach (ReferenceDescription referenceDescription in (List<ReferenceDescription>) descriptionCollection)
          {
            TreeNode node1 = new TreeNode();
            node1.Text = referenceDescription.BrowseName.Name.ToString();
            node1.Tag = (object) referenceDescription;
            this.CustomizeTreeNode(ref node1);
            TreeNode node2 = new TreeNode("dummy");
            node1.Nodes.Add(node2);
            nodes.Add(node1);
          }
          this.SortTreeNode(parentNode == null ? this.tvBrowseTree.Nodes : parentNode.Nodes);
          this.tvBrowseTree.EndUpdate();
        }
        else
        {
          int num2 = (int) TxMessageBox.Show(CUiECExternalConnectionsResourceTable.BROWSE_FAILED + num1.ToString(), CUiECExternalConnectionsResourceTable.BROWSE_OPC_UA_SERVER, MessageBoxButtons.OK, MessageBoxIcon.Hand);
        }
      }
      catch (Exception ex)
      {
        num1 = -1;
        int num3 = (int) TxMessageBox.Show(CUiECExternalConnectionsResourceTable.BROWSE_ERROR, CUiECExternalConnectionsResourceTable.BROWSE_OPC_UA_SERVER, MessageBoxButtons.OK, MessageBoxIcon.Hand);
      }
    }
    Cursor.Current = Cursors.Default;
    return num1;
  }

  private void CustomizeTreeNode(ref TreeNode node)
  {
    ReferenceDescription tag = (ReferenceDescription) node.Tag;
    if (tag == null)
      return;
    if (NodeId.op_Equality(ExpandedNodeId.ToNodeId(tag.TypeDefinition, (NamespaceTable) null), (object) 61U))
      node.ImageKey = "treefolder";
    else if (NodeId.op_Equality(ExpandedNodeId.ToNodeId(ExpandedNodeId.op_Implicit(tag.ReferenceTypeId), (NamespaceTable) null), (object) 46U))
    {
      node.ImageKey = "property";
    }
    else
    {
      NodeClass nodeClass = tag.NodeClass;
      if (nodeClass <= 16 /*0x10*/)
      {
        switch (nodeClass - 1)
        {
          case 0:
            node.ImageKey = "treefolder";
            goto label_22;
          case 1:
            node.ImageKey = "variable";
            goto label_22;
          case 2:
            break;
          case 3:
            node.ImageKey = "method";
            goto label_22;
          default:
            if (nodeClass != 8)
            {
              if (nodeClass == 16 /*0x10*/)
              {
                node.ImageKey = "variabletype";
                goto label_22;
              }
              break;
            }
            node.ImageKey = "objecttype";
            goto label_22;
        }
      }
      else if (nodeClass != 32 /*0x20*/)
      {
        if (nodeClass != 64 /*0x40*/)
        {
          if (nodeClass == 128 /*0x80*/)
          {
            node.ImageKey = "view";
            goto label_22;
          }
        }
        else
        {
          node.ImageKey = "datatype";
          goto label_22;
        }
      }
      else
      {
        node.ImageKey = "reftype";
        goto label_22;
      }
      node.ImageKey = "error";
    }
label_22:
    node.SelectedImageKey = node.ImageKey;
    node.StateImageKey = node.ImageKey;
  }

  private void SortTreeNode(TreeNodeCollection nodes)
  {
    if (nodes.Count <= 1)
      return;
    TreeNode[] treeNodeArray = new TreeNode[nodes.Count];
    nodes.CopyTo((Array) treeNodeArray, 0);
    Array.Sort<TreeNode>(treeNodeArray, new Comparison<TreeNode>(this.CompareTreeNodes));
    nodes.Clear();
    nodes.AddRange(treeNodeArray);
  }

  private int CompareTreeNodes(TreeNode nodeA, TreeNode nodeB)
  {
    int num = Decimal.Compare((Decimal) this.tvBrowseTree.ImageList.Images.IndexOfKey(nodeA.ImageKey), (Decimal) this.tvBrowseTree.ImageList.Images.IndexOfKey(nodeB.ImageKey));
    return num != 0 ? num : string.Compare(nodeA.Text, nodeB.Text, true);
  }

  private void tvBrowseTree_BeforeExpand(object sender, TreeViewCancelEventArgs e)
  {
    TreeNode node = e.Node;
    if (node == null)
      return;
    this.Browse(node);
  }

  private void tvBrowseTree_AfterSelect(object sender, TreeViewEventArgs e)
  {
    this.OnSelectionChanged(e.Node);
  }

  private void tvBrowseTree_ItemDrag(object sender, ItemDragEventArgs e)
  {
    if (!(((TreeNode) e.Item).Tag.GetType() == typeof (ReferenceDescription)))
      return;
    ReferenceDescription tag = (ReferenceDescription) ((TreeNode) e.Item).Tag;
    if (tag.NodeId.IsAbsolute || tag.NodeClass != 2)
      return;
    this.tvBrowseTree.DoDragDrop((object) tag.NodeId.ToString(), DragDropEffects.Copy);
  }

  private void tvBrowseTree_MouseDown(object sender, MouseEventArgs e)
  {
    if (e.Button != MouseButtons.Right)
      return;
    TreeNode nodeAt = this.tvBrowseTree.GetNodeAt(e.X, e.Y);
    this.tvBrowseTree.SelectedNode = nodeAt;
    if (nodeAt == null || !(nodeAt.Tag is ReferenceDescription tag))
      return;
    if (tag.NodeClass == 2)
      this.m_CurrentNode = nodeAt;
    else if (tag.NodeClass == 1)
      this.m_CurrentNode = nodeAt;
    else
      this.m_CurrentNode = nodeAt;
  }

  public TreeView BrowseTree
  {
    get => this.tvBrowseTree;
    set => this.tvBrowseTree = value;
  }

  protected override void Dispose(bool disposing)
  {
    if (disposing && this.components != null)
      this.components.Dispose();
    base.Dispose(disposing);
  }

  private void InitializeComponent()
  {
    this.components = (IContainer) new System.ComponentModel.Container();
    ComponentResourceManager componentResourceManager = new ComponentResourceManager(typeof (BrowseControl));
    this.tvBrowseTree = new TreeView();
    this.TreeViewIL = new ImageList(this.components);
    this.SuspendLayout();
    this.tvBrowseTree.AllowDrop = true;
    this.tvBrowseTree.Dock = DockStyle.Fill;
    this.tvBrowseTree.ImageIndex = 0;
    this.tvBrowseTree.ImageList = this.TreeViewIL;
    this.tvBrowseTree.Location = new Point(0, 0);
    this.tvBrowseTree.Name = "tvBrowseTree";
    this.tvBrowseTree.SelectedImageIndex = 0;
    this.tvBrowseTree.Size = new Size(150, 150);
    this.tvBrowseTree.TabIndex = 0;
    this.tvBrowseTree.BeforeExpand += new TreeViewCancelEventHandler(this.tvBrowseTree_BeforeExpand);
    this.tvBrowseTree.ItemDrag += new ItemDragEventHandler(this.tvBrowseTree_ItemDrag);
    this.tvBrowseTree.AfterSelect += new TreeViewEventHandler(this.tvBrowseTree_AfterSelect);
    this.tvBrowseTree.MouseDown += new MouseEventHandler(this.tvBrowseTree_MouseDown);
    this.TreeViewIL.ImageStream = (ImageListStreamer) componentResourceManager.GetObject("TreeViewIL.ImageStream");
    this.TreeViewIL.TransparentColor = Color.Transparent;
    this.TreeViewIL.Images.SetKeyName(0, "error");
    this.TreeViewIL.Images.SetKeyName(1, "method");
    this.TreeViewIL.Images.SetKeyName(2, "object");
    this.TreeViewIL.Images.SetKeyName(3, "objecttype");
    this.TreeViewIL.Images.SetKeyName(4, "property");
    this.TreeViewIL.Images.SetKeyName(5, "reftype");
    this.TreeViewIL.Images.SetKeyName(6, "type");
    this.TreeViewIL.Images.SetKeyName(7, "variable");
    this.TreeViewIL.Images.SetKeyName(8, "variabletype");
    this.TreeViewIL.Images.SetKeyName(9, "view");
    this.TreeViewIL.Images.SetKeyName(10, "datatype");
    this.TreeViewIL.Images.SetKeyName(11, "browse");
    this.TreeViewIL.Images.SetKeyName(12, "treefolder");
    this.AutoScaleDimensions = new SizeF(6f, 13f);
    this.AutoScaleMode = AutoScaleMode.Font;
    this.Controls.Add((Control) this.tvBrowseTree);
    this.Name = nameof (BrowseControl);
    this.ResumeLayout(false);
    this.tvBrowseTree.BackColor = TxStatusColor.Color("ImageBackground");
    this.tvBrowseTree.ForeColor = TxStatusColor.Color("ImageText");
  }

  public delegate void SelectionChangedEventHandler(TreeNode selectedNode);

  public delegate void UpdateStatusLabelEventHandler(string strMessage, bool bSuccess);
}
